clear all 
set more off 

	** Specify path to replication folder 
	global path "XXX"

	** Subfolders
	global fig "$path/Outputs/Figures"
	global int "${path}/Data/Intermediate"

	** Codes to install
	/* 
	ssc install scheme-modern, replace
	ssc install ftools, replace 
	ssc install reghdfe, replace 
	ssc install parmest, replace
	ssc install outreg2, replace
	ssc install winsor2, replace
	ssc install ineqdeco, replace
	help synth_runner
	ssc install synth, replace
	ssc install ereplace, replace
	*/

	** Set folder for tables
	cd "${fig}"

	* Scheme for figures
	set scheme s2color

	****************************************************************************
****************************************************************************
** MAIN PAPER
****************************************************************************
****************************************************************************

************
** Figure 1: Tertiary Enrollment and Democracy: 1970-2019
************
{
	use "${int}/wdi_fh.dta",clear

	binscatter se_ter fiw,absorb(decade) ytitle("Tertiary Gross Enrollment Rate (%)") ylabel(,angle(horizontal)) xlabel(0 `" `"Most"' `"Democratic"' "' 1 `" `"Least"' `"Democratic"' "') xscale(range(0 1.05)) xtitle(Freedom in the World index) name(fig1,replace)
	
	graph export "Figure_Paper_1.pdf", as(pdf) replace
}

************
** Figure 2: Enrollment and funding across education levels
************
{
	** Panel A
	use "${int}/figures", clear
	twoway 	(connected ind_sh_bas614 year,msymbol(square) color(maroon)) ///
			(connected ind_sh_med1519 year,msymbol(diamond) color(forest_green)) ///
			(connected ind_sh_uni2024 year,msymbol(circle_hollow) color(dkorange)) if year>=1965&year<=1980, ///
	ytitle(Gross Enrollment Rate (1970=100)) ylabel(,angle(horizontal)) xtitle(Year) xline(1973, lcolor(red)) legend(r(1) pos(6) symxsize(*.4) order(1 "Primary (97%)" 2 "Secondary (33%)" 3 "Higher (9%)")) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig2a,replace) 
	
	graph export "Figure_Paper_2_A.pdf", as(pdf) replace

	** Panel B 
	
	use "${int}/EXTRA/Data_Figures_2_B", clear 
	tsset country cohort
	gen age_21=cohort+21
	keep if age_21>=1964 & age_21<=1981

	keep if latam == 1 | countrycode=="CHL"
	gen chile=(countrycode=="CHL")
	collapse (mean) university, by(chile age_21)
	twoway (connected university age_21 if chile==1)  (connected university age_21 if chile==0), ylabel(,angle(horizontal)) xline(1973.5) xtitle("Age 21 cohort") ytitle("Share with complete college") scheme(s2color) legend(r(1) pos(6) symxsize(*.4) region(col(white)) order(1 "Chile" 2 "Latin America")) graphregion(fcolor(white) ///
			lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig2b,replace)
	graph export "Figure_Paper_2_B.pdf", replace
	
	** Panel C
	use "${int}/figures", clear
	twoway 	(connected gasto_educ_gdp year,msymbol(triangle)) ///
			(connected sh_bas_educ year,msymbol(square)) ///
			(connected sh_med_educ year,msymbol(diamond)) ///
			(connected sh_sup_educ year,msymbol(circle_hollow)) if year>=1969&year<=1980, ///
			ytitle(Educational Expenditure Shares) ylabel(,angle(horizontal)) xtitle(Year) xline(1973, lcolor(red)) ///
			legend(r(1) pos(6) symxsize(*.5) order(1 "Educ/GDP" 2 "Primary/Educ" 3 "Secondary/Educ" 4 "Higher/Educ")) ///
			graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig2c,replace)

	graph export "Figure_Paper_2_C.pdf", as(pdf) replace

	** Panel D
	use "${int}/figures", clear
	twoway 	(connected openings_ths year,msymbol(square) lcolor(sienna) mcolor(sienna) ) ///
			(connected paatakers_ths year,msymbol(triangle) lcolor(teal) mcolor(teal)) ///
			(connected applicants_ths year,msymbol(circle) mcolor(ltblue) lcolor(ltblue)) if year>=1967&year<=1980, ///
			ytitle(Number (X 1000)) ylabel(, angle(horizontal)) xtitle(Year) xline(1973, lcolor(red)) xlabel(1965(5)1980) ///
			legend(order(2 "Test-takers" 3 "Applicants" 1 "Openings") rows(1) pos(6) symxsize(0.5)) graphregion(fcolor(white) lcolor(white) ///
			ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig2d,replace)
	
	graph export "Figure_Paper_2_D.pdf", as(pdf) replace
}

************
** Figure 3: College openings and enrollment by field
************
{
	** Panel A
	use "${int}/figures", clear
	graph hbar (asis) d_open_agriculture d_open_socialsciences d_open_law d_open_art_architecture ///
		d_open_health d_open_education d_open_technology_engineering d_open_humanities d_open_naturalsciences_math if year==1980, ///
		showyvars yvaroptions(relabel(1 "Agriculture (0.06)" 2 "Social Sciences (0.15)" 3 "Law (0.02)" ///
		4 "Art / Architecture (0.04)" 5 "Health (0.09)" 6 "Education (0.30)" 7 "Engineering (0.30)" 8 "Humanities (0.02)" ///
		9 "Natural Sciences (0.03)")) legend(off) ytitle("Change in Openings 1973-1980 (%)")  yline(-30,lpattern(dash)) ///
		bar(1, fcolor(maroon%100) lcolor(maroon)) bar(2, fcolor(maroon%90) lcolor(maroon)) bar(3, fcolor(maroon%80) ///
		lcolor(maroon)) bar(4, fcolor(maroon%70) lcolor(maroon)) bar(5, fcolor(maroon%60) lcolor(maroon)) bar(6, fcolor(maroon%50) lcolor(maroon)) ///
		bar(7, fcolor(maroon%40) lcolor(maroon)) bar(8, fcolor(maroon%30) lcolor(maroon)) bar(9, fcolor(green%20) lcolor(maroon))  ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig3a,replace)
	
	graph export "Figure_Paper_3_A.pdf", as(pdf) replace

	** Panel B
	use "${int}/figures", clear
	keep if sh_matr_agro!=.
	keep sh_matr_agro sh_matr_art sh_matr_nat sh_matr_soc sh_matr_law sh_matr_huma sh_matr_educ sh_matr_inge sh_matr_salud year
	reshape long sh_matr_, i(year) j(field) string
	gen area=1 if field=="agro"
	replace area=2 if field=="soc"
	replace area=3 if field=="law"
	replace area=4 if field=="art"
	replace area=5 if field=="salud"
	replace area=6 if field=="educ"
	replace area=7 if field=="inge"
	replace area=8 if field=="huma"
	replace area=9 if field=="nat"
	graph bar (asis) sh_matr_, over(year) over(area, relabel(1 "Agriculture" 2 "Social Science" 3 "Law" ///
		4 "Art/Architecture" 5 "Health" 6 "Education" 7 "Engineering" 8 "Humanities" 9 "Natural Science") ///
		label(labsize(small) angle(45))) asyvars bar(1, fcolor(ltblue) fintensity(inten30) lcolor(ltblue)) ///
		bar(2, fcolor(sand) fintensity(inten60) lcolor(sand)) bar(3, fcolor(sienna) fintensity(inten80) lcolor(sienna)) ///
		ytitle(Percentage of Total Enrollment (%)) ylabel(, angle(horizontal)) legend(rows(1) pos(6)) graphregion(fcolor(white) lcolor(white) ///
		ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig3b,replace)
	
	graph export "Figure_Paper_3_B.pdf", as(pdf) replace
}

************
** Figure 4: Characterization of incoming class by family background
************
{
	** Panel A
	use "${int}/figures", clear
	keep year father*
	keep if year==1976|year==1981
	reshape long father_, i(year) j(educ) string
	rename father_ enrol
	graph bar (asis) enrol, over(year) over(educ, relabel(1 "{&le} Primary" 2 "Secondary" 3 "Higher")) ///
		asyvars ytitle(Percentage of Incoming Class (%)) ylabel(, angle(horizontal)) bar(1, color(red) fcolor(%50))  ///
		bar(2, color(navy) fcolor(%80))  graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
		plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) scheme(s1color) name(fig4a,replace)
	
	graph export "Figure_Paper_4_A.pdf", as(pdf) replace

	** Panel B
	use "${int}/figures", clear
	keep year i_paa*
	keep if year==1976|year==1981
	reshape long i_paa_avg,i(year) j(ocup) string
	gen occ=1 if ocup=="_profesor"
	replace occ=2 if ocup=="_gerente"
	replace occ=3 if ocup=="_tecnico"
	replace occ=4 if ocup=="_oficina"
	replace occ=5 if ocup=="_pequeno"
	replace occ=6 if ocup=="_artesano"
	replace occ=7 if ocup=="_service"
	replace occ=8 if ocup=="_urbcali"
	replace occ=9 if ocup=="_agrcali"
	replace occ=10 if ocup=="_urbncal"
	replace occ=11 if ocup=="_agrncal"
	sort occ year
	gen occ1=occ
	replace occ1=occ1-0.15 if year==1976
	replace occ1=occ1+0.15 if year==1981
	twoway (bar i_paa_avg occ1 if year==1976, barwidth(0.3) fintensity(50) color(red)) ///
		(bar i_paa_avg occ1 if year==1981, barwidth(0.3) color(navy) fintensity(80)), ///
		xlabel(1 "Univ. faculty" 2 "Managers" 3 "Technicians" 4 "Office workers" 5 "Small business" ///
		6 "Craft workers" 7 "Personal services" 8 "BC qualified (urb)" 9 "BC qualified (agr)" 10 "BC unqualified (urb)" ///
		11 "BC unqualified (agr)",angle(45) labsize(small)) xtitle("") legend(order(1 "1976" 2 "1981") pos(6) row(1)) ///
		ytitle(Average PAA score (Faculty=100)) ylabel(,angle(horizontal)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ///
		ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig4b,replace) 
	
	graph export "Figure_Paper_4_B.pdf", as(pdf) replace
}

************
** Figure 5: Income inequality
************
{
	** Panel A
	use "${int}/Data_Figure_5.dta", clear

	twoway (connected pct_decil5 year)  ///
			(connected pct_decil24 year,msymbol(square)) ///
			(connected pct_decil1 year,msymbol(triangle)) if year>=1960, ///
			legend(order(1 "Top 20%" 2 "Middle 60%" 3 "Bottom 20%") pos(6) r(1)) xline(1973, lcolor(red)) ///
			xline(1990, lcolor(green)) ylabel(,angle(horizontal)) ytitle(Income Share) xtitle("") graphregion(fcolor(white) ///
			lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig5a,replace)

	graph export "Figure_Paper_5_A.pdf", as(pdf) replace

	** Panel B
	use "${int}/Data_Figure_5.dta", clear
	drop if gini == .
	twoway (connected gini year), xline(1973, lcolor(red)) xline(1990, lcolor(green)) ///
		ylabel(,angle(horizontal)) ytitle(Gini Coefficient) xtitle("") graphregion(fcolor(white) lcolor(white) ///
		ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig5b,replace)

	graph export "Figure_Paper_5_B.pdf", as(pdf) replace
}

************
** Figure 6: Visualization of kink in college enrollment and labor market outcomes
************
{
	use "${int}/censo1992.dta",clear
	
	preserve	
	collapse (mean) any_media_4p any_college, by(age_21)

	gen age_21_pre=cond(age_21<=1972,age_21 - 1972,0)
	gen age_21_post=cond(age_21>1972,age_21 - 1972,0)
	gen age_21_all=age_21 - 1972

	rename any_college any_college_uncond
	
	foreach x in any_media_4p any_college_uncond {			
		reg `x' age_21_pre age_21_post
		predict `x'_hat_pre
		gen `x'_hat_post=_b[_cons]+(_b[age_21_pre]*age_21_all)
		
		twoway (scatter `x' age_21) (lfit `x'_hat_pre age_21 if age_21<1973, lcolor(green)) ///
				(lfit `x'_hat_pre age_21 if age_21>=1972, lcolor(gray)) ///				
				(lfit `x'_hat_post age_21 if age_21>=1972, lcolor(green) lpattern(dash)), ///
				xlabel(1964(2)1981) xline(1972.5, lcolor(red)) ylabel(`ylabel',angle(horizontal)) ///
				legend(order(1 "Mean" 2 "Fitted values (64-72)" 4 "Fitted values (73-81)") r(1) pos(6)) ///
				xtitle("Age 21 cohort") `ytitle' graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
				plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig6_`x',replace)
		graph export "Figure_Paper_6_`x'.pdf", as(pdf) replace						
		drop *hat*
	}	

	restore

	keep if any_media_4p==1

	collapse (mean) any_college lfp Docc2 median_wage_9200_3 unemployed, by(age_21)

	gen age_21_pre=cond(age_21<=1972,age_21 - 1972,0)
	gen age_21_post=cond(age_21>1972,age_21 - 1972,0)
	gen age_21_all=age_21 - 1972
	
	foreach x in any_college lfp Docc2 median_wage_9200_3 unemployed{	
		reg `x' age_21_pre age_21_post
		predict `x'_hat_pre
		gen `x'_hat_post=_b[_cons]+(_b[age_21_pre]*age_21_all)
		
		twoway (scatter `x' age_21) (lfit `x'_hat_pre age_21 if age_21<1973, lcolor(green)) ///
				(lfit `x'_hat_pre age_21 if age_21>=1972, lcolor(gray)) ///				
				(lfit `x'_hat_post age_21 if age_21>=1972, lcolor(green) lpattern(dash)), ///
				xlabel(1964(2)1981) xline(1972.5, lcolor(red)) ylabel(`ylabel',angle(horizontal)) ///
				legend(order(1 "Mean" 2 "Fitted values (64-72)" 4 "Fitted values (73-81)") pos(6) r(1)) ///
				xtitle("Age 21 cohort") `ytitle' graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
				plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig6_`x',replace)
		graph export "Figure_Paper_6_`x'.pdf", as(pdf) replace						
		drop *hat*			
	}

	use "${int}/casen_all.dta", clear
	
	keep if any_media_4p==1
	collapse (mean) ytotalaj_r_w, by(age_21)
		
	gen age_21_pre=cond(age_21<=1972,age_21 - 1972,0)
	gen age_21_post=cond(age_21>1972,age_21 - 1972,0)
	gen age_21_all=age_21 - 1972
		
	foreach x in ytotalaj_r_w {			
		reg `x' age_21_pre age_21_post
		predict `x'_hat_pre
		gen `x'_hat_post=_b[_cons]+(_b[age_21_pre]*age_21_all)
		
		twoway (scatter `x' age_21) (lfit `x'_hat_pre age_21 if age_21<1973, lcolor(green)) ///
				(lfit `x'_hat_pre age_21 if age_21>=1972, lcolor(gray)) ///				
				(lfit `x'_hat_post age_21 if age_21>=1972, lcolor(green) lpattern(dash)), ///
				xlabel(1964(2)1981) xline(1972.5, lcolor(red)) ylabel(`ylabel',angle(horizontal)) ///
				legend(order(1 "Mean" 2 "Fitted values (64-72)" 4 "Fitted values (73-81)") r(1) pos(6)) ///
				xtitle("Age 21 cohort") `ytitle' graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
				plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig6_`x',replace)
		graph export "Figure_Paper_6_`x'.pdf", as(pdf) replace						
		drop *hat*
	}
}

************
** Figure 7: Visualization of Kink: Household Wealth and Income
************
{
	use "${int}/censo1992.dta",clear
	
	keep if any_media_4p==1

	collapse (mean) Dqui5 Dqui2_4 Dqui1, by(age_21)

	gen age_21_pre=cond(age_21<=1972,age_21 - 1972,0)
	gen age_21_post=cond(age_21>1972,age_21 - 1972,0)
	gen age_21_all=age_21 - 1972
	
	foreach x in Dqui5 Dqui2_4 Dqui1{	
		reg `x' age_21_pre age_21_post
		predict `x'_hat_pre
		gen `x'_hat_post=_b[_cons]+(_b[age_21_pre]*age_21_all)
		
		twoway (scatter `x' age_21) (lfit `x'_hat_pre age_21 if age_21<1973, lcolor(green)) ///
				(lfit `x'_hat_pre age_21 if age_21>=1972, lcolor(gray)) ///				
				(lfit `x'_hat_post age_21 if age_21>=1972, lcolor(green) lpattern(dash)), ///
				xlabel(1964(2)1981) xline(1972.5, lcolor(red)) ylabel(`ylabel',angle(horizontal)) ///
				legend(order(1 "Mean" 2 "Fitted values (64-72)" 4 "Fitted values (73-81)") pos(6) r(1)) ///
				xtitle("Age 21 cohort") `ytitle' graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
				plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig7_`x',replace)
		graph export "Figure_Paper_7_`x'.pdf", as(pdf) replace						
		drop *hat*			
	}

	use "${int}/casen_all.dta", clear
	
	keep if any_media_4p==1
	collapse (mean) Dqaut_5 Dqaut_2_4 Dqaut_1, by(age_21)
		
	gen age_21_pre=cond(age_21<=1972,age_21 - 1972,0)
	gen age_21_post=cond(age_21>1972,age_21 - 1972,0)
	gen age_21_all=age_21 - 1972
		
	foreach x in Dqaut_5 Dqaut_2_4 Dqaut_1{			
		reg `x' age_21_pre age_21_post
		predict `x'_hat_pre
		gen `x'_hat_post=_b[_cons]+(_b[age_21_pre]*age_21_all)
		
		twoway (scatter `x' age_21) (lfit `x'_hat_pre age_21 if age_21<1973, lcolor(green)) ///
				(lfit `x'_hat_pre age_21 if age_21>=1972, lcolor(gray)) ///				
				(lfit `x'_hat_post age_21 if age_21>=1972, lcolor(green) lpattern(dash)), ///
				xlabel(1964(2)1981) xline(1972.5, lcolor(red)) ylabel(`ylabel',angle(horizontal)) ///
				legend(order(1 "Mean" 2 "Fitted values (64-72)" 4 "Fitted values (73-81)") r(1) pos(6)) ///
				xtitle("Age 21 cohort") `ytitle' graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
				plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig7_`x',replace)
		graph export "Figure_Paper_7_`x'.pdf", as(pdf) replace						
		drop *hat*
	}
}

************
** Figure 8: Synthetic control
************
{
	foreach yvar in full_college lfp occ_prof { 

		use "${int}/clean-ipums-panel", clear	

		keep if cohort <= 1981-21

		bys CountryID year: egen non_m = count(cohort) 
		tab non_m, m
		 
		egen min_cohorts = min(non_m) 
		tab min_cohorts
		sort CountryID cohort 
		bys CountryID: gen drp = _n
		tab drp
		drop if drp > min_cohorts 
		drop non_m
		 
		replace `yvar' = 0 if  `yvar' == .

		xtset CountryID cohort

		*=********************=*
		*=* Synth
		*=********************=*
		  
		bys CountryID: egen non_m = count(`yvar')			
		tab non_m, m
		drop if non_m == 0 // Incomplete data. 
		drop non_m
			
		sum CountryID if countrycode=="CHL"
		local c=r(mean)
		tempfile keepfile
		synth_runner `yvar' `yvar' ///
							`yvar'(1932) `yvar'(1934) `yvar'(1936) ///
							`yvar'(1938) `yvar'(1940) `yvar'(1942) ///
							`yvar'(1944) `yvar'(1946) `yvar'(1948) ///
							`yvar'(1950) `yvar'(1952) ///
							,	///
							trunit(`c') trperiod(1952) keep(`keepfile') 
						  
		qui merge 1:1 CountryID	 cohort using `keepfile', nogenerate
		qui gen `yvar'_synth = `yvar'-effect

		///POST RMSPE 
		gen post_d= (`yvar'-`yvar'_synth)^2
		bys CountryID: egen post_e = mean(post_d) if  cohort>=1952 & cohort<=1960
		bys CountryID: gen post_e2 = sqrt(post_e) if  cohort>=1952 & cohort<=1960		   
				   
		///	P-value
		preserve
			qui g r_PP=post_e2/pre_rmspe
			*Ratio just for chile
			qui g Chl=r_PP if CountryID==`c'
			qui g all=1
			qui ereplace Chl = mean(Chl)
			*Effect mean for post-treatment
			qui bys CountryID: egen post_effect = mean(effect) if cohort >= 1952 & cohort <= 1960
			*Effect just for chile
			qui g Chl_eff=post_effect if CountryID==`c'
			qui ereplace Chl_eff = mean(Chl_eff)
			sum Chl_eff 
			local effectmean=r(mean)	
			///p-values
			qui g I=1 if Chl<r_PP & post_effect < 0
			qui recode I .=0
			*Adjusting
			qui collapse (max) I (mean) effect, by(CountryID)
			sum I
			local pvalue1=r(mean)
		restore	  
			
		/// Draw Fig

		local pvalue: dis %4.3f `pvalue1'
		local beta: dis %4.3f `effectmean'

		#d ; 
			replace cohort=cohort+21; 
			local yr_tx = 1952+21;
			 
			twoway (connected `yvar' cohort 
						if CountryID == `c' & 
						   cohort >= 1952 & 
						   cohort <= 1998, msize(small) msymbol(square) mcolor(forest_green) lcolor(forest_green))
				   (line `yvar'_synth cohort 
						if CountryID == `c' & 
						cohort >= 1952 & 
						cohort <= 1998, 
								lc(forest_green) lp(dash))
				   , 
				   graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
				   plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
				   scale(1.4) 
				   legend(label(1 "Chile") label(2 "Synthetic"))  
				   xtitle("Age 21 cohort", size(small)) 
				   ytitle("`:var label `yvar''", size(small)) 
				   ylabel(,angle(horizontal))
				   note( "{it: Effect:} `beta'" "{it: p-value:} `pvalue'", 	
				   ring(1) pos(1) size(*1)) 		 
				   xline(`yr_tx', lp("_"))		   
				   name(fig8_`yvar', replace);

			graph export "Figure_Paper_8_`yvar'.pdf", replace; 
		#d cr
	}
}
************
** Figure 9: Political outcomes: 1988 plebiscite
************
{
	** Panel B
	use "${int}/Data_Figure_6.dta", clear

	reg ins_pleb age_21_pre age_21_post
	predict ins_pleb_hat_pre
	gen ins_pleb_hat_post=_b[_cons]+(_b[age_21_pre]*age_21_all)
	
	twoway (scatter ins_pleb age_21) (lfit ins_pleb_hat_pre age_21 if age_21<1973, lcolor(green)) ///
			(lfit ins_pleb_hat_pre age_21 if age_21>=1972, lcolor(gray)) ///				
			(lfit ins_pleb_hat_post age_21 if age_21>=1972, lcolor(green) lpattern(dash)), ///
			xlabel(1964(2)1981) xline(1972.5, lcolor(red)) ylabel(,angle(horizontal))ytitle(Registered Voters,angle(horizontal)) ///
			legend(order(1 "Mean" 2 "Fitted values (64-72)" 4 "Fitted values (73-81)") pos(6) r(1)) ///
			xtitle("Age 21 cohort") `ytitle' graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
			plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(fig9a,replace)

	graph export "Figure_Paper_9_A.pdf", as(pdf) replace						
	
	** Panel B
	use "${int}/Data_Plebiscite.dta", clear

	binscatter VoteShareNo estimate1_adj_std if sample==1, ylabel(,angle(horizontal)) ytitle("NO vote share (%)") xtitle("Standardized kink in college enrollment after military coup") name(fig9b,replace)

	graph export "Figure_Paper_9_B.pdf", as(pdf) replace						
}

****************************************************************************
****************************************************************************
** ONLINE APPENDIX
****************************************************************************
****************************************************************************

************
** Figure A.1: Other outcomes: Lower levels
************
{
	** Panel A
	use "${int}/figures", clear

	graph bar (asis) ind_establ_pre ind_establ_bas ind_establ_med ///
		if year==1973|year==1977, over(year)  bar(1, fintensity(inten80)) bar(2, fintensity(inten40)) ///
		bar(3, fintensity(inten20)) blabel(bar, format(%3.0f)) ytitle(Number of Schools (1969=100)) ///
		ylabel(,angle(horizontal)) legend(r(1) order(1 "Early" "(48)" 2 "Primary" "(7302)" 3 "Secondary" "(717)") pos(6)) ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA1a)
	
	graph export "Figure_Appendix_1_A.pdf", as(pdf) replace

	** Panel B
	use "${int}/figures", clear

	twoway (connected pct_desayuno year,msymbol(triangle)) (connected pct_almuerzo year,msymbol(square)) ///
		if year>=1965&year<=1980, ytitle(Share of Students in Primary) ylabel(,angle(horizontal)) xtitle(Year) ///
		xline(1973, lcolor(red)) legend(r(1) pos(6) order(1 "Breakfast" 2 "Lunch")) graphregion(fcolor(white) lcolor(white) ///
		ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA1b)

	graph export "Figure_Appendix_1_B.pdf", as(pdf) replace
}

************
** Figure A.2: Further evidence on supply and demand for college
************
{
	** Panel A
	use "${int}/figures", clear

	twoway (connected open_publicas_ths year) (connected open_privadas_ths year, msymbol(triangle)) ///
		if year>=1970&year<=1980, ytitle(Openings (x1000)) ylabel(, angle(horizontal)) xtitle(Year) ///
		xline(1973, lcolor(red)) xlabel(1970(5)1980) legend(order(1 "Public" 2 "Private") row(1) pos(6)) ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
		lcolor(white) ifcolor(white) ilcolor(white)) name(figA2a,replace)

	graph export "Figure_Appendix_2_A.pdf", as(pdf) replace

	** Panel B
	use "${int}/figures", clear

	twoway (connected openings_ths year,msymbol(square) lcolor(sienna) mcolor(sienna) ) ///
		(connected regopenings_ths year,msymbol(triangle) lcolor(brown) mcolor(brown)) ///
		if year>=1965&year<=1981, ytitle(Number (x 1000)) ylabel(, angle(horizontal)) xtitle(Year) ///
		xline(1973, lcolor(red)) xlabel(1965(5)1980) legend(order(3 "Applicants" 1 "Openings" 2 "Openings (regular)") ///
		rows(1) pos(6) symxsize(0.5)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
		plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA2b,replace)

	graph export "Figure_Appendix_2_B.pdf", as(pdf) replace	
}

************
** Figure A.3: Post-enrollment outcomes
************
{
	** Panel A
	use "${int}/figures", clear

	twoway (connected sh_grad year) (connected grad_rate year,yaxis(2) msymbol(triangle)) ///
		if year>=1964&year<=1981,ylabel(, angle(horizontal) axis(1)) ylabel(, angle(horizontal) axis(2)) ///
		xtitle("") ytitle(Graduating/Enrollment (year), axis(1)) ytitle(4+ years/Enrollment (cohort), axis(2)) ///
		legend(order(1 "Year (left axis)" 2 "Age 21 cohort (right axis)") pos(6) row(1)) ///
		xline(1973, lcolor(red)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
		plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA3a,replace)

	graph export "Figure_Appendix_3_A.pdf", as(pdf) replace	

	** Panel B
	use "${int}/casen_all.dta", clear

	reghdfe ln_ytotalaj_r_w any_college_1964-any_college_1981 i.edad i.year if any_media_4p ==1, a(id_comuna_female) cl(comuna) 
	tempfile figA3b
	parmest,saving(`figA3b', replace)

	use `figA3b',clear
	keep in 1/18
	gen year=substr(parm,13,4)
	destring year,replace
		
	twoway (scatter estimate year) (rcap min95 max95 year), ///
		ytitle(ln Total Income) ylabel(, angle(horizontal)) xtitle(Age 21 cohort) ///
		xline(1973) legend(order(1 "Point estimate" 2 "95% confidence interval" ) pos(6) row(1)) ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
		plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA3a,replace)

	graph export "Figure_Appendix_3_B.pdf", as(pdf) replace
}

************
** Figure A.4: Age distribution of first-year college students
************
{
	use "${int}/EXTRA/college_age_first_year", clear // Source: 1960 population census (INE, 1965) + Schiefelbein (1976) + Echeverria (1982, cuadro 1.2, p. 39)

	egen college_yr1_total=sum(college_yr1),by(year)
	gen sh_college_yr1=college_yr1/college_yr1_total
	mean age if year==1960&college_yr1>0 [aweight=college_yr1]
	local temp: di %3.1fc e(b)[1,1]
	mean age if year==1970&college_yr1>0 [aweight=college_yr1]
	local temp1: di %3.1fc e(b)[1,1]
	mean age if year==1975&college_yr1>0 [aweight=college_yr1]
	local temp2: di %3.1fc e(b)[1,1]
	
	graph bar (asis) sh_college_yr1 if college_yr1>0, over(year) ///
		over(age, relabel(1 "<18" 2 "18" 3 "19" 4 "20" 5 "21" 6 "22" 7 "23" 8 "24" 9 "25-29" 10 ">29")) ///
		asyvars bar(1, fintensity(15)) bar(2, fintensity(50)) bar(3, fintensity(85)) ///
		ytitle(Share of first-year college students) ylabel(, angle(horizontal)) b1title(Age) ///
		text(0.35 65 "{bf:Average:}" "1960: `temp'" "1970: `temp1'" "1975: `temp2'") legend(rows(1) pos(6)) ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
		lcolor(white) ifcolor(white) ilcolor(white)) name(figA4,replace)

	graph export "Figure_Appendix_4.pdf", as(pdf) replace	
}

************
** Figure A.5: College enrollment: Different sources
************
{

	** Panel A
	use "${int}/cohort_all_sources.dta",clear

	twoway 	(connected any_media_4p_1992 age_21, msize(small)) ///
			(connected any_media_4p_2002 age_21, msize(small) msymbol(triangle)) ///
			(connected any_media_4p_2017 age_21, msize(small) msymbol(square) mcolor(forest_green) lcolor(forest_green)) ///
			(connected any_media_4p_casen age_21, msize(small) msymbol(diamond)) if age_21>=1960&age_21<=1990, ytitle("Share with full secondary") ///
			ylabel(, angle(horizontal) ) xlabel(1960 `" `"1960"' `"(1939)"' "' 1970 `" `"1970"' `"(1949)"' "' 1980 `" `"1980"' `"(1959)"' "' 1990 `" `"1990"' `"(1969)"' "', labsize(small) grid) ///
			xtitle(" " "Age 21 cohort (birth year)") xline(1972.5,lcolor(red)) xline(1964 1981,lcolor(navy) lpattern(dash)) ///
			graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
			legend(order(1 "Census 92" 2 "Census 02" 3 "Census 17" 4 "CASEN") rows(1) pos(6) symxsize(*.5) keygap(*.25)) name(figA5a,replace) 
	
	graph export "Figure_Appendix_5_A.pdf", as(pdf) replace	

	** Panel B
	use "${int}/cohort_all_sources.dta",clear

	twoway 	(connected any_college_fm_1992 age_21, msize(small)) ///
			(connected any_college_fm_2002 age_21, msize(small) msymbol(triangle)) ///
			(connected any_college_fm_2017 age_21, msize(small) msymbol(square) mcolor(forest_green) lcolor(forest_green)) ///
			(connected any_college_fm_casen age_21, msize(small) msymbol(diamond)) if age_21>=1960&age_21<=1990, ytitle("Share with any college| full secondary") ///
			ylabel(, angle(horizontal) ) xlabel(1960 `" `"1960"' `"(1939)"' "' 1970 `" `"1970"' `"(1949)"' "' 1980 `" `"1980"' `"(1959)"' "' 1990 `" `"1990"' `"(1969)"' "', labsize(small) grid) ///
			xtitle(" " "Age 21 cohort (birth year)") xline(1972.5,lcolor(red)) xline(1964 1981,lcolor(navy) lpattern(dash)) ///
			graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
			legend(order(1 "Census 92" 2 "Census 02" 3 "Census 17" 4 "CASEN") rows(1) pos(6) symxsize(*.5) keygap(*.25)) name(figA5b,replace) 
	
	graph export "Figure_Appendix_5_B.pdf", as(pdf) replace	

}

************
** Figure A.6: Visualization of kink: Occupational choice
************
{
	use "${int}/censo1992.dta",clear

	drop Docc0 Docc99
	keep if any_media_4p==1
	collapse (mean) Docc*, by(age_21)

	gen age_21_pre=cond(age_21<=1972,age_21 - 1972,0)
	gen age_21_post=cond(age_21>1972,age_21 - 1972,0)
	gen age_21_all=age_21 - 1972
	
	foreach x of varlist Docc*{	
		reg `x' age_21_pre age_21_post
		predict `x'_hat_pre
		gen `x'_hat_post=_b[_cons]+(_b[age_21_pre]*age_21_all)
		
		twoway (scatter `x' age_21) (lfit `x'_hat_pre age_21 if age_21<1973, lcolor(green)) ///
				(lfit `x'_hat_pre age_21 if age_21>=1972, lcolor(gray)) ///				
				(lfit `x'_hat_post age_21 if age_21>=1972, lcolor(green) lpattern(dash)), ///
				xlabel(1964(2)1981) xline(1972.5, lcolor(red)) ylabel(`ylabel',angle(horizontal)) ///
				legend(order(1 "Mean" 2 "Fitted values (64-72)" 4 "Fitted values (73-81)") pos(6) r(1)) ///
				xtitle("Age 21 cohort") `ytitle' graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
				plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA6_`x',replace)
		graph export "Figure_Appendix_6_`x'.pdf", as(pdf) replace						
		drop *hat*			
	}	
}

************
** Figure A.7: Visualization of kink: Occupational income score for other wage samples
************
{
	use "${int}/censo1992.dta",clear
	
	keep if any_media_4p==1
	collapse (mean) median_wage_9296_3 median_wage_9217_3 , by(age_21)

	gen age_21_pre=cond(age_21<=1972,age_21 - 1972,0)
	gen age_21_post=cond(age_21>1972,age_21 - 1972,0)
	gen age_21_all=age_21 - 1972
	
	foreach x in median_wage_9296_3 median_wage_9217_3 {	
		reg `x' age_21_pre age_21_post
		predict `x'_hat_pre
		gen `x'_hat_post=_b[_cons]+(_b[age_21_pre]*age_21_all)
		
		twoway (scatter `x' age_21) (lfit `x'_hat_pre age_21 if age_21<1973, lcolor(green)) ///
				(lfit `x'_hat_pre age_21 if age_21>=1972, lcolor(gray)) ///				
				(lfit `x'_hat_post age_21 if age_21>=1972, lcolor(green) lpattern(dash)), ///
				xlabel(1964(2)1981) xline(1972.5, lcolor(red)) ylabel(`ylabel',angle(horizontal)) ///
				legend(order(1 "Mean" 2 "Fitted values (64-72)" 4 "Fitted values (73-81)") pos(6) r(1)) ///
				xtitle("Age 21 cohort") `ytitle' graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
				plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA7_`x',replace)
		graph export "Figure_Appendix_7_`x'.pdf", as(pdf) replace						
		drop *hat*			
	}
}

************
** Figure A.8: Synthetic control: Robustness
************
{
******************************
*	NO LATAM
******************************	

foreach yvar in full_college lfp occ_prof { 

	use "${int}/clean-ipums-panel", clear	

	keep if cohort <= 1981-21

	bys CountryID year: egen non_m = count(cohort) 
	tab non_m, m
	 
	egen min_cohorts = min(non_m) 
	tab min_cohorts
	sort CountryID cohort 
	bys CountryID: gen drp = _n
	tab drp
	drop if drp > min_cohorts 
	drop non_m
	 
	replace `yvar' = 0 if  `yvar' == .

	*drop latam
	keep if latam==0 | countrycode=="CHL"

	tab cohort

	xtset CountryID cohort

	*=********************=*
	*=* Synth
	*=********************=*
	  
	bys CountryID: egen non_m = count(`yvar')			
	tab non_m, m
	drop if non_m == 0 // Incomplete data. 
	drop non_m
		
	sum CountryID if countrycode=="CHL"
	local c=r(mean)
	tempfile keepfile
	synth_runner `yvar' `yvar' ///
						`yvar'(1932) `yvar'(1934) `yvar'(1936) ///
						`yvar'(1938) `yvar'(1940) `yvar'(1942) ///
						`yvar'(1944) `yvar'(1946) `yvar'(1948) ///
						`yvar'(1950) `yvar'(1952) ///
						,	///
						trunit(`c') trperiod(1952) keep(`keepfile') 
					  
	qui merge 1:1 CountryID	 cohort using `keepfile', nogenerate
	qui gen `yvar'_synth = `yvar'-effect

	///POST RMSPE 
	gen post_d= (`yvar'-`yvar'_synth)^2
	bys CountryID: egen post_e = mean(post_d) if  cohort>=1952 & cohort<=1960
	bys CountryID: gen post_e2 = sqrt(post_e) if  cohort>=1952 & cohort<=1960		   
			   
	///	P-value
	preserve
		qui g r_PP=post_e2/pre_rmspe
		*Ratio just for chile
		qui g Chl=r_PP if CountryID==`c'
		qui g all=1
		qui ereplace Chl = mean(Chl)
		*Effect mean for post-treatment
		qui bys CountryID: egen post_effect = mean(effect) if cohort >= 1952 & cohort <= 1960
		*Effect just for chile
		qui g Chl_eff=post_effect if CountryID==`c'
		qui ereplace Chl_eff = mean(Chl_eff)
		sum Chl_eff 
		local effectmean=r(mean)	
		///p-values
		qui g I=1 if Chl<r_PP & post_effect < 0
		qui recode I .=0
		*Adjusting
		qui collapse (max) I (mean) effect, by(CountryID)
		sum I
		local pvalue1=r(mean)
	restore	  
		
	/// Draw Fig

	local pvalue: dis %4.3f `pvalue1'
	local beta: dis %4.3f `effectmean'

	#d ; 
		replace cohort=cohort+21; 
		local yr_tx = 1952+21;
		 
		twoway (connected `yvar' cohort 
					if CountryID == `c' & 
					   cohort >= 1952 & 
					   cohort <= 1998, msize(small) msymbol(square) mcolor(forest_green) lcolor(forest_green))
			   (line `yvar'_synth cohort 
					if CountryID == `c' & 
					cohort >= 1952 & 
					cohort <= 1998, 
							lc(forest_green) lp(dash))
			   , 
			   graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
			   plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
			   scale(1.4) 
			   legend(label(1 "Chile") label(2 "Synthetic"))  
			   xtitle("Age 21 cohort", size(small)) 
			   ytitle("`:var label `yvar''", size(small)) 
			   ylabel(,angle(horizontal))
			   note( "{it: Effect:} `beta'" "{it: p-value:} `pvalue'", 	
			   ring(1) pos(1) size(*1)) 		 
			   xline(`yr_tx', lp("_"))		   
			   name(figA8_`yvar'_nolat, replace);

		graph export "Figure_Appendix_8_`yvar'_nolat.pdf", replace; 
	#d cr
}


******************************
*	Post 1960
******************************	

foreach yvar in full_college lfp occ_prof { 

	use "${int}/clean-ipums-panel", clear	

	keep if cohort <= 1981-21
	drop if cohort <= 1960-21

	bys CountryID year: egen non_m = count(cohort) 
	tab non_m, m
	 
	egen min_cohorts = min(non_m) 
	tab min_cohorts
	sort CountryID cohort 
	bys CountryID: gen drp = _n
	tab drp
	drop if drp > min_cohorts 
	drop non_m
	 
	replace `yvar' = 0 if  `yvar' == .

	xtset CountryID cohort

	*=********************=*
	*=* Synth
	*=********************=*
	  
	bys CountryID: egen non_m = count(`yvar')			
	tab non_m, m
	drop if non_m == 0 // Incomplete data. 
	drop non_m
		
	sum CountryID if countrycode=="CHL"
	local c=r(mean)
	tempfile keepfile
	synth_runner `yvar' `yvar'(1940(1)1952) ///
				    `yvar'(1940) `yvar'(1942) ///
				    `yvar'(1944) `yvar'(1946) `yvar'(1948) ///
				    `yvar'(1950) `yvar'(1952) ///
				    ,	///
				    trunit(`c') trperiod(1952) keep(`keepfile') 
					  
	qui merge 1:1 CountryID	 cohort using `keepfile', nogenerate
	qui gen `yvar'_synth = `yvar'-effect

	///POST RMSPE 
	gen post_d= (`yvar'-`yvar'_synth)^2
	bys CountryID: egen post_e = mean(post_d) if  cohort>=1952 & cohort<=1960
	bys CountryID: gen post_e2 = sqrt(post_e) if  cohort>=1952 & cohort<=1960		   
			   
	///	P-value
	preserve
		qui g r_PP=post_e2/pre_rmspe
		*Ratio just for chile
		qui g Chl=r_PP if CountryID==`c'
		qui g all=1
		qui ereplace Chl = mean(Chl)
		*Effect mean for post-treatment
		qui bys CountryID: egen post_effect = mean(effect) if cohort >= 1952 & cohort <= 1960
		*Effect just for chile
		qui g Chl_eff=post_effect if CountryID==`c'
		qui ereplace Chl_eff = mean(Chl_eff)
		sum Chl_eff 
		local effectmean=r(mean)	
		///p-values
		qui g I=1 if Chl<r_PP & post_effect < 0
		qui recode I .=0
		*Adjusting
		qui collapse (max) I (mean) effect, by(CountryID)
		sum I
		local pvalue1=r(mean)
	restore	  
		
	/// Draw Fig

	local pvalue: dis %4.3f `pvalue1'
	local beta: dis %4.3f `effectmean'

	#d ; 
		replace cohort=cohort+21; 
		local yr_tx = 1952+21;
		 
		twoway (connected `yvar' cohort 
					if CountryID == `c' & 
					   cohort >= 1952 & 
					   cohort <= 1998, msize(small) msymbol(square) mcolor(forest_green) lcolor(forest_green))
			   (line `yvar'_synth cohort 
					if CountryID == `c' & 
					cohort >= 1952 & 
					cohort <= 1998, 
							lc(forest_green) lp(dash))
			   , 
			   graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
			   plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))    
			   scale(1.4) 
			   legend(label(1 "Chile") label(2 "Synthetic"))  
			   xtitle("Age 21 cohort", size(small)) 
			   ytitle("`:var label `yvar''", size(small)) 
			   ylabel(,angle(horizontal))
			   note( "{it: Effect:} `beta'" "{it: p-value:} `pvalue'", 	
			   ring(1) pos(1) size(*1)) 		 
			   xline(`yr_tx', lp("_"))		   
			   name(figA8_`yvar'_post60, replace);

		graph export "Figure_Appendix_8_`yvar'_post60.pdf", replace; 
	#d cr
}


******************************
*	No autocracy
******************************	

foreach yvar in full_college lfp occ_prof { 

	use "${int}/clean-ipums-panel", clear	

	keep if cohort <= 1981-21

	bys CountryID year: egen non_m = count(cohort) 
	tab non_m, m
	 
	egen min_cohorts = min(non_m) 
	tab min_cohorts
	sort CountryID cohort 
	bys CountryID: gen drp = _n
	tab drp
	drop if drp > min_cohorts 
	drop non_m
	 
	replace `yvar' = 0 if  `yvar' == .
	
	*drop autocracies
	drop if dict_5090==1 & countrycode != "CHL"

	xtset CountryID cohort

	*=********************=*
	*=* Synth
	*=********************=*
	  
	bys CountryID: egen non_m = count(`yvar')			
	tab non_m, m
	drop if non_m == 0 // Incomplete data. 
	drop non_m
		
	sum CountryID if countrycode=="CHL"
	local c=r(mean)
	tempfile keepfile
	synth_runner `yvar' `yvar' ///
						`yvar'(1932) `yvar'(1934) `yvar'(1936) ///
						`yvar'(1938) `yvar'(1940) `yvar'(1942) ///
						`yvar'(1944) `yvar'(1946) `yvar'(1948) ///
						`yvar'(1950) `yvar'(1952) ///
						,	///
						trunit(`c') trperiod(1952) keep(`keepfile') 
					  
	qui merge 1:1 CountryID	 cohort using `keepfile', nogenerate
	qui gen `yvar'_synth = `yvar'-effect

	///POST RMSPE 
	gen post_d= (`yvar'-`yvar'_synth)^2
	bys CountryID: egen post_e = mean(post_d) if  cohort>=1952 & cohort<=1960
	bys CountryID: gen post_e2 = sqrt(post_e) if  cohort>=1952 & cohort<=1960		   
			   
	///	P-value
	preserve
		qui g r_PP=post_e2/pre_rmspe
		*Ratio just for chile
		qui g Chl=r_PP if CountryID==`c'
		qui g all=1
		qui ereplace Chl = mean(Chl)
		*Effect mean for post-treatment
		qui bys CountryID: egen post_effect = mean(effect) if cohort >= 1952 & cohort <= 1960
		*Effect just for chile
		qui g Chl_eff=post_effect if CountryID==`c'
		qui ereplace Chl_eff = mean(Chl_eff)
		sum Chl_eff 
		local effectmean=r(mean)	
		///p-values
		qui g I=1 if Chl<r_PP & post_effect < 0
		qui recode I .=0
		*Adjusting
		qui collapse (max) I (mean) effect, by(CountryID)
		sum I
		local pvalue1=r(mean)
	restore	  
		
	/// Draw Fig

	local pvalue: dis %4.3f `pvalue1'
	local beta: dis %4.3f `effectmean'

	#d ; 
		replace cohort=cohort+21; 
		local yr_tx = 1952+21;
		 
		twoway (connected `yvar' cohort 
					if CountryID == `c' & 
					   cohort >= 1952 & 
					   cohort <= 1998, msize(small) msymbol(square) mcolor(forest_green) lcolor(forest_green))
			   (line `yvar'_synth cohort 
					if CountryID == `c' & 
					cohort >= 1952 & 
					cohort <= 1998, 
							lc(forest_green) lp(dash))
			   , 
			   graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
			   plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))    
			   scale(1.4) 
			   legend(label(1 "Chile") label(2 "Synthetic"))  
			   xtitle("Age 21 cohort", size(small)) 
			   ytitle("`:var label `yvar''", size(small)) 
			   ylabel(,angle(horizontal))
			   note( "{it: Effect:} `beta'" "{it: p-value:} `pvalue'", 	
			   ring(1) pos(1) size(*1)) 		 
			   xline(`yr_tx', lp("_"))		   
			   name(figA8_`yvar'_naut, replace);

		graph export "Figure_Appendix_8_`yvar'_naut.pdf", replace; 
	#d cr
}
}

************
** Figure A.9: Robustness to different bandwidths
************
{
	use "${int}/Data_Figure_A9_1992.dta",clear
	
	local if_s3 if age_21>=1967 & age_21<=1978 & any_media_4p ==1 
	local if_s4 if age_21>=1966 & age_21<=1979 & any_media_4p ==1 
	local if_s5 if age_21>=1965 & age_21<=1980 & any_media_4p ==1 
	local if_s6 if age_21>=1964 & age_21<=1981 & any_media_4p ==1
	local if_s7 if age_21>=1963 & age_21<=1982 & any_media_4p ==1 
	local if_s8 if age_21>=1962 & age_21<=1983 & any_media_4p ==1 
	local if_s9 if age_21>=1961 & age_21<=1984 & any_media_4p ==1

	foreach x in any_college lfp unemployed Docc2 {
		forvalues bw=3/9{
			reghdfe `x' a21_yr a21_yr_post `if_s`bw'', a(id_comuna_female) cl(comuna_nac) nocons
			tempfile `x's`bw'
			parmest,saving(``x's`bw'', replace) idstr(`bw'_`x') emac(depvar)			
		}
	}
	
	use "${int}/Data_Figure_A9_casen.dta",clear

	foreach x in ytotalaj_r_w{
		forvalues bw=3/9{
			reghdfe `x' a21_yr a21_yr_post `if_s`bw'', a(id_comuna_female) cl(comuna) nocons
			tempfile `x's`bw'
			parmest,saving(``x's`bw'', replace) idstr(`bw'_`x') emac(depvar)			
		}
	}
	
	foreach x in any_college lfp unemployed Docc2 ytotalaj_r_w {
		clear
		forvalues bw=3/9{
			append using ``x's`bw''
		}
		drop if parm=="_cons"
		gen bw=substr(idstr,1,1)
		destring bw,replace
		replace bw=bw-2
		label define bw 1 "67-78" 2 "66-79" 3 "65-80" 4 "64-81" 5 "63-82" 6 "62-83" 7 "61-84" 
		label values bw bw

		if "`x'"=="any_college"{
			local ylabel "-0.05(0.01)0"
		}
		else if "`x'"=="lfp"{
			local ylabel "-0.015(0.005)0"
		}
		else if "`x'"=="unemployed"{
			local ylabel "0(0.001)0.005"
		}
		else if "`x'"=="Docc2"{
			local ylabel "-0.025(0.005)0"
		}
		else if "`x'"=="ytotalaj_r_w"{
			local ylabel "-15(5)0"
		}
		twoway (scatter estimate bw if parm=="a21_yr",msymbol(circle)) ///
			(scatter estimate bw if parm=="a21_yr_post",msymbol(square)) ///
			(rcap min95 max95 bw,lcolor(gs10)), ytitle(Kink Estimate) yline(0) ylabel(, angle(horizontal) format(%4.3f)) ///
			xtitle(Cohorts) xlabel(1(1)7, valuelabel) legend(order(1 "Yr Age 21" 2 " Yr Age 21 x 1(Dictatorship)" 3 "95% C.I.") r(1) pos(6)) ///
			graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ///
			ifcolor(white) ilcolor(white)) name(figA9_`x')
		graph export "Figure_Appendix_9_`x'.pdf", as(pdf) replace
	}		
}

************
** Figure A.10: Wealth and income distributions: Different bandwidths
************
{
	use "${int}/Data_Figure_A9_1992.dta",clear
	
	local if_s3 if age_21>=1967 & age_21<=1978 & any_media_4p ==1 
	local if_s4 if age_21>=1966 & age_21<=1979 & any_media_4p ==1 
	local if_s5 if age_21>=1965 & age_21<=1980 & any_media_4p ==1 
	local if_s6 if age_21>=1964 & age_21<=1981 & any_media_4p ==1
	local if_s7 if age_21>=1963 & age_21<=1982 & any_media_4p ==1 
	local if_s8 if age_21>=1962 & age_21<=1983 & any_media_4p ==1 
	local if_s9 if age_21>=1961 & age_21<=1984 & any_media_4p ==1

	foreach x in Dqui5 Dqui2_4 Dqui1{
		forvalues bw=3/9{
			reghdfe `x' a21_yr a21_yr_post `if_s`bw'', a(id_comuna_female) cl(comuna_nac) nocons
			tempfile `x's`bw'
			parmest,saving(``x's`bw'', replace) idstr(`bw'_`x') emac(depvar)			
		}
	}

	use "${int}/Data_Figure_A9_casen.dta",clear

	foreach x in Dqaut_5 Dqaut_2_4 Dqaut_1{
		forvalues bw=3/9{
			reghdfe `x' a21_yr a21_yr_post `if_s`bw'', a(id_comuna_female) cl(comuna) nocons
			tempfile `x's`bw'
			parmest,saving(``x's`bw'', replace) idstr(`bw'_`x') emac(depvar)			
		}
	}
	
	foreach x in Dqui5 Dqui2_4 Dqui1 Dqaut_5 Dqaut_2_4 Dqaut_1{
		clear
		forvalues bw=3/9{ 
			append using ``x's`bw''
		}
		drop if parm=="_cons"
		gen bw=substr(idstr,1,1)
		destring bw,replace
		replace bw=bw-2
		label define bw 1 "67-78" 2 "66-79" 3 "65-80" 4 "64-81" 5 "63-82" 6 "62-83" 7 "61-84" 
		label values bw bw

		if "`x'"=="Dqui5"{
			local ylabel "-0.02(0.005)0"
		}
		else if "`x'"=="Dqui2_4"{
			local ylabel "0(0.005)0.02"
		}
		else if "`x'"=="Dqui1"{
			local ylabel "0(0.0005)0.0015"
		}
		else if "`x'"=="Dqaut_5"{
			local ylabel "-0.01(0.002)0"
		}		
		else if "`x'"=="Dqaut_2_4"{
			local ylabel "-0.002(0.001)0.006"
		}		
		else if "`x'"=="Dqaut_1"{
			local ylabel "-0.002(0.001)0.006"
		}
		twoway (scatter estimate bw if parm=="a21_yr",msymbol(circle)) ///
				(scatter estimate bw if parm=="a21_yr_post",msymbol(square)) ///
				(rcap min95 max95 bw,lcolor(gs10)), ytitle(Kink Estimate) yline(0) ylabel(, angle(horizontal) format(%4.3f)) ///
				xtitle(Cohorts) xlabel(1(1)7, valuelabel) legend(order(1 "Yr Age 21" 2 " Yr Age 21 x 1(Dictatorship)" 3 "95% C.I.") r(1) pos(6)) ///
				graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ///
				ifcolor(white) ilcolor(white)) name(figA10`x',replace)
		graph export "Figure_Appendix_10_`x'.pdf", as(pdf) replace
	}		
}

************
** Figure A.11: Macroeconomic conditions
************
{
	use "${int}/macro.dta",clear
	rename age_21 year
	foreach x of varlist gdp youth public{
		replace `x'=`x'*100
	}
	
	** Panel A
	twoway (connected gdp_pc_gr year) , xline(1973) yline(0,lcolor(gs10)) ///
		ylabel(,angle(horizontal)) ytitle("GDP per capita growth rate (%)") ///
		graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
		plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA11a,replace)

	graph export "Figure_Appendix_11_A.pdf", as(pdf) replace

	** Panel B 
	twoway (connected gvt year), ylabel(0(10)30,angle(horizontal)) ///
		 ytitle("Government spending (% of GDP)") xline(1973) ///
		 graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
		 plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA11b,replace)

	graph export "Figure_Appendix_11_B.pdf", as(pdf) replace

	** Panel C 
	twoway (connected youth_u year), ylabel(0(10)30,angle(horizontal)) ///
		ytitle("Unemployment rate (%)") xline(1973) graphregion(fcolor(white) ///
		lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
		lcolor(white) ifcolor(white) ilcolor(white)) name(figA11c,replace)

	graph export "Figure_Appendix_11_C.pdf", as(pdf) replace

	** Panel D
	twoway (connected public year), ylabel(2(2)8,angle(horizontal)) ///
		ytitle("Public employment (%)") xline(1973) graphregion(fcolor(white) ///
		lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
		lcolor(white) ifcolor(white) ilcolor(white)) name(figA11d,replace)

	graph export "Figure_Appendix_11_D.pdf", as(pdf) replace

	** Panel E
	twoway (connected sindicatos year), ///
		ytitle("New Trade Unions Created") xline(1973) graphregion(fcolor(white) ///
		lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
		lcolor(white) ifcolor(white) ilcolor(white)) name(figA11e,replace)
		
	graph export "Figure_Appendix_11_E.pdf", as(pdf) replace
}

************
** Figure A.12: International Migration
************
{
	** Panel A
	use "${int}/students_abroad.dta",clear
	
	twoway (connected tot_students year, mlabel(tot_paises)) if year>=1965&year<=1985&tot_paises>=15, ///
			ylabel(,angle(horizontal)) ytitle("Students (thousands)") xtitle(Year) graphregion(fcolor(white) ///
			lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ///
			ilcolor(white)) name(figA12a,replace)
	
	graph export "Figure_Appendix_12_A.pdf", as(pdf) replace

	** Panel B
	use "${int}/censo1992.dta",clear

	keep if any_media_4p==1

	collapse (mean) migration_abroad, by(age_21)

	gen age_21_pre=cond(age_21<=1972,age_21 - 1972,0)
	gen age_21_post=cond(age_21>1972,age_21 - 1972,0)
	gen age_21_all=age_21 - 1972

	reg migration_abroad age_21_pre age_21_post
	predict migration_abroad_hat_pre
	gen migration_abroad_hat_post=_b[_cons]+(_b[age_21_pre]*age_21_all)
	
	twoway (scatter migration_abroad age_21) (lfit migration_abroad_hat_pre age_21 if age_21<1973, lcolor(green)) ///
			(lfit migration_abroad_hat_pre age_21 if age_21>=1972, lcolor(gray)) ///				
			(lfit migration_abroad_hat_post age_21 if age_21>=1972, lcolor(green) lpattern(dash)), ///
			xlabel(1964(2)1981) xline(1972.5, lcolor(red)) ylabel(`ylabel',angle(horizontal)) ///
			legend(order(1 "Mean" 2 "Fitted values (64-72)" 4 "Fitted values (73-81)") r(1) pos(6)) ///
			xtitle("Age 21 cohort") ytitle(Share living abroad in 1987) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
			plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA12b,replace)

	graph export "Figure_Appendix_12_B.pdf", as(pdf) replace

	** Panel C
	use "${int}/chileans_abroad.dta",clear

	twoway (connected sh_total age_21,msymbol(circle)) ///
			(connected sh_media age_21,msymbol(square)) (connected sh_college age_21,msymbol(triangle)) ///
			if age_21<1987, xlabel(1952 "1950-1954" 1957 "1955-1959" 1962 "1960-1964" 1967 "1965-1969" ///
			1972 "1970-1974" 1977 "1975-1979" 1982 "1980-1984", angle(45)) xtitle(Age 21) ///
			ylabel(,angle(horixontal)) ytitle(Migrant Share) legend(order(1 "Total" 2 "Full Secondary" 3 "Any College") r(1) pos(6)) ///
			graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))

	graph export "Figure_Appendix_12_C.pdf", as(pdf) replace
		
	** Panel D
	use "${int}/voters_abroad.dta", clear

	reg migrant age_21_pre age_21_post
	predict migrant_hat_pre
	gen migrant_hat_post=_b[_cons]+(_b[age_21_pre]*age_21_all)
	
	twoway (scatter migrant age_21) (lfit migrant_hat_pre age_21 if age_21<1973, lcolor(green)) ///
			(lfit migrant_hat_pre age_21 if age_21>=1972, lcolor(gray)) ///				
			(lfit migrant_hat_post age_21 if age_21>=1972, lcolor(green) lpattern(dash)), ///
			xlabel(1964(2)1981) xline(1972.5, lcolor(red)) ylabel(`ylabel',angle(horizontal)) ///
			legend(order(1 "Mean" 2 "Fitted values (64-72)" 4 "Fitted values (73-81)") r(1) pos(6)) ///
			xtitle("Age 21 cohort") ytitle("Share of Voters Abroad (2017)") graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) ///
			plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA12d,replace)

	graph export "Figure_Appendix_12_D.pdf", as(pdf) replace		
}

************
** Figure A.13: Military conscription
************
{
	use "${int}/enlistment.dta", clear

	two (connect Enlisted Year , msize(small) msymbol(square) mcolor(forest_green) lcolor(forest_green)), scale(1.2) xlabel(1965(2)1980) ///
			ylabel(,angle(horizontal))ytitle("Enlisted (thousands)") graphregion(fcolor(white) ///
			lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) name(figA13,replace)

	graph export "Figure_Appendix_13.pdf", as(pdf) replace	
}

************
** Figure A.14: Robustness to different kink points
************
{
	use "${int}/censo1992.dta",clear

	drop a21*
	
	forvalues x =1970(1)1974{
	
		g a21_yr = age_21-`x'
		g a21_yr_post = a21_yr*(age_21>`x')
	
		foreach y in any_college lfp unemployed Docc2{
			reghdfe `y' a21_yr a21_yr_post if any_media_4p ==1, a(id_comuna_female) cl(comuna_nac) nocons
			tempfile kink_`y'_`x'
			parmest,saving(`kink_`y'_`x'', replace) idstr(`y'_`x') emac(depvar)			
		}
		drop a21*
	}	
	
	use "${int}/casen_all.dta", clear
	
	drop a21*
	
	forvalues x =1970(1)1974{
	
		g a21_yr = age_21-`x'
		g a21_yr_post = a21_yr*(age_21>`x')
		reghdfe ytotalaj_r_w a21_yr a21_yr_post if any_media_4p ==1, a(id_comuna_female year year) cl(comuna)
		tempfile kink_ytotalaj_r_w_`x'
		parmest,saving(`kink_ytotalaj_r_w_`x'', replace) idstr(ytotalaj_r_w_`x') emac(depvar)			
		drop a21*
	}

	foreach y in any_college lfp unemployed Docc2 ytotalaj_r_w {
		clear
		forvalues x=1970/1974{ 
			append using `kink_`y'_`x''
		}
		drop if parm=="_cons"
		gen kink=substr(idstr,-4,4)
		destring kink,replace
		replace kink=kink-0.05 if parm=="a21_yr"
		replace kink=kink+0.05 if parm=="a21_yr_post"
		twoway (scatter estimate kink if parm=="a21_yr",msymbol(circle)) ///
				(scatter estimate kink if parm=="a21_yr_post",msymbol(square)) (rcap min95 max95 kink,lcolor(gs10)), ///
				ytitle(Estimated cohort trend and change) yline(0) ylabel(, angle(horizontal)) xtitle(Kink point) ///
				legend(order(1 "Yr Age 21" 2 " Yr Age 21 x 1(Dictatorship)" 3 "95% C.I.") r(1) pos(6)) ///
				graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
				lcolor(white) ifcolor(white) ilcolor(white)) name(figA14_`y',replace)
		graph export "Figure_Appendix_14_`y'.pdf", as(pdf) replace	
	}	
}

************
** Figure A.15: Wealth and income distributions: Different kink points
************
{
	use "${int}/censo1992.dta",clear

	drop a21*
	
	forvalues x =1970(1)1974{
	
		g a21_yr = age_21-`x'
		g a21_yr_post = a21_yr*(age_21>`x')
	
		foreach y in Dqui5 Dqui2_4 Dqui1{
			reghdfe `y' a21_yr a21_yr_post if any_media_4p ==1, a(id_comuna_female) cl(comuna_nac) nocons
			tempfile kink_`y'_`x'
			parmest,saving(`kink_`y'_`x'', replace) idstr(`y'_`x') emac(depvar)			
		}
		drop a21*
	}	
	
	use "${int}/casen_all.dta", clear
	
	drop a21*
	
	forvalues x =1970(1)1974{
	
		g a21_yr = age_21-`x'
		g a21_yr_post = a21_yr*(age_21>`x')
		foreach y in Dqaut_5 Dqaut_2_4 Dqaut_1{
			reghdfe `y' a21_yr a21_yr_post if any_media_4p ==1, a(id_comuna_female year) cl(comuna) nocons
			tempfile kink_`y'_`x'
			parmest,saving(`kink_`y'_`x'', replace) idstr(`y'_`x') emac(depvar)			
		}
		drop a21*
	}

	foreach y in Dqui5 Dqui2_4 Dqui1 Dqaut_5 Dqaut_2_4 Dqaut_1{
		clear
		forvalues x=1970/1974{ 
			append using `kink_`y'_`x''
		}
		drop if parm=="_cons"
		gen kink=substr(idstr,-4,4)
		destring kink,replace
		replace kink=kink-0.05 if parm=="a21_yr"
		replace kink=kink+0.05 if parm=="a21_yr_post"
		twoway (scatter estimate kink if parm=="a21_yr",msymbol(circle)) ///
				(scatter estimate kink if parm=="a21_yr_post",msymbol(square)) (rcap min95 max95 kink,lcolor(gs10)), ///
				ytitle(Estimated cohort trend and change) yline(0) ylabel(, angle(horizontal)) xtitle(Kink point) ///
				legend(order(1 "Yr Age 21" 2 " Yr Age 21 x 1(Dictatorship)" 3 "95% C.I.") r(1) pos(6)) ///
				graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) ///
				lcolor(white) ifcolor(white) ilcolor(white)) name(figA15_`y',replace)
		graph export "Figure_Appendix_15_`y'.pdf", as(pdf) replace	
	}	
}


